package com.feilong.edgeAI.view;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.Xfermode;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.Collections;

public class AnimatedRecordingView extends BaseSurfaceView {
    private static final String TAG = "SiriView";
    private static final int MAX_GAPS = 300;
    private static final float ANIM_SCALE_TIME = 400.0F;
    private static final int LOADING_WIDTH = 80;
    private static final int LOADING_HEIGHT = 10;
    private static final int LOGO_LEFT_MARGIN = 60;
    private static int LOCATION_LEFT_RIGHT_OFFSET = 100;
    private static final int COLOR_RED = Color.parseColor("#f22b2b");
    private static final int COLOR_RED_END = Color.parseColor("#f22b2b");
    private static final int COLOR_GREEN = Color.parseColor("#2bf3af");
    private static final int COLOR_GREEN_END = Color.parseColor("#2bf3af");
    private static final int COLOR_BLUE = Color.parseColor("#2BAAF3");
    private static final int COLOR_BLUE_END = Color.parseColor("#2BAAF3");
    private static final int COLOR_LOADING = Color.parseColor("#007AFE");
    private static final int STATE_SLEEP = 0;
    private static final int STATE_WORK = 1;
    private static final int STATE_SEARCH = 2;
    static float[] mSamplingX = new float[301];
    static float[] mMapX = new float[301];
    Context mContext;
    ArrayList<SiriCursorView> mCursorList;
    float mWidth;
    static float mGap;
    float mHeight;
    int mState;
    Paint mPaint;
    private static final Xfermode xfermode;
    BlurMaskFilter mMaskFilter;
    Path mPathmBitmapLoadingTop;
    Path mPathmBitmapLoadingBottom;
    ArrayList<Integer> mShuffleList;
    Bitmap mBitmapLoading;
    Bitmap mBitmapLogo;
    long mCurentTime;
    Matrix mMatrix;
    RectF mBitmapLoadingRectf;
    RadialGradient mRadialGradient;
    RadialGradient mRadialGradientScale;
    RectF mRectFScale;
    boolean leftToRight;
    final float MAX_ANIM_STOP_INDEX;
    final float MAX_ANIM_LOADING_INDEX;
    float mStopAnimIndex;
    boolean isStopLeft;
    float mLoadingAnimIndex;
    int mControl;
    Runnable mDitherRunnable;

    public AnimatedRecordingView(Context context) {
        this(context, (AttributeSet)null);
    }

    public AnimatedRecordingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnimatedRecordingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mState = 0;
        this.leftToRight = true;
        this.MAX_ANIM_STOP_INDEX = 20.0F;
        this.MAX_ANIM_LOADING_INDEX = 45.0F;
        this.mStopAnimIndex = 0.0F;
        this.isStopLeft = false;
        this.mLoadingAnimIndex = 0.0F;
        this.mControl = 0;
        this.mDitherRunnable = new Runnable() {
            public void run() {
                for(int i = 0; i < 9; ++i) {
                    SiriCursorView view = (SiriCursorView) AnimatedRecordingView.this.mCursorList.get(i);
                    view.randomLocationAndOffset();
                }

            }
        };
        this.init(context);
    }

    public boolean isWorking() {
        return this.mState != 0;
    }

    void initCanvas(Canvas canvas) {
        if (this.mWidth == 0.0F) {
            this.mWidth = (float)canvas.getWidth();
            mGap = this.mWidth / 300.0F;
            this.mHeight = (float)canvas.getHeight();
            this.mRadialGradient = new RadialGradient(this.mWidth / 2.0F, this.mHeight / 2.0F, this.mWidth / 2.0F, new int[]{-1, COLOR_BLUE, COLOR_LOADING}, (float[])null, TileMode.CLAMP);
            this.mRadialGradientScale = new RadialGradient(this.mWidth / 2.0F, this.mHeight / 2.0F, this.mWidth / 2.0F, new int[]{COLOR_LOADING, COLOR_BLUE, -1}, (float[])null, TileMode.CLAMP);
            this.mBitmapLoadingRectf = new RectF((float)LOCATION_LEFT_RIGHT_OFFSET, this.mHeight / 2.0F - 3.0F, this.mWidth - (float)LOCATION_LEFT_RIGHT_OFFSET, this.mHeight / 2.0F + 3.0F);

            for(int i = 0; i <= 300; ++i) {
                float x = (float)i * mGap;
                mSamplingX[i] = x;
                mMapX[i] = x / this.mWidth * 20.0F - 10.0F;
            }

            this.caculateLocations();
        }

    }

    void drawWave(Canvas canvas, float volume) {
        this.mPaint.setShader(this.mRadialGradient);
        canvas.drawOval(this.mBitmapLoadingRectf, this.mPaint);
        this.mPaint.setShader((Shader)null);
        canvas.clipRect((float)LOCATION_LEFT_RIGHT_OFFSET, 0.0F, (float)((int)(this.mWidth - (float)LOCATION_LEFT_RIGHT_OFFSET)), this.mHeight);

        for(int i = 0; i < 9; ++i) {
            Integer index = (Integer)this.mShuffleList.get(i);
            SiriCursorView view = (SiriCursorView)this.mCursorList.get(index);
            view.drawWave(canvas, this.mPaint, volume, this.mHeight / 2.0F, 0.0F, this.mWidth);
        }

    }

    protected void onRender(Canvas canvas, float volume) {
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, 3));
        this.initCanvas(canvas);
        canvas.drawColor(0, Mode.CLEAR);
        if (this.mState == 1) {
            this.drawWave(canvas, volume);
        } else if (this.mState == 2) {
            this.drawSearch(canvas);
        } else if (this.mState == 0) {
            this.drawStopLogo(canvas);
        }

    }

    void drawStopLogo(Canvas canvas) {
        canvas.save();
        float left;
        if (20.0F >= this.mStopAnimIndex) {
            left = this.getInterpolation(this.mStopAnimIndex / 20.0F);
            this.mPaint.setAlpha((int)(160.0F + 95.0F * left));
            ++this.mStopAnimIndex;
        }

        left = this.mWidth / 2.0F - (float)(this.mBitmapLogo.getWidth() / 2);
        if (this.isStopLeft) {
            left = 60.0F;
        }

        canvas.drawBitmap(this.mBitmapLogo, left, this.mHeight / 2.0F - (float)this.mBitmapLogo.getHeight() / 2.0F, this.mPaint);
        canvas.restore();
    }

    void drawSearch(Canvas canvas) {
        if (0L == this.mCurentTime) {
            this.mCurentTime = System.currentTimeMillis();
        } else {
            long time_offset = System.currentTimeMillis() - this.mCurentTime;
            float progress;
            float interpolation;
            float dx;
            if ((float)time_offset <= 400.0F) {
                this.mPaint.setShader(this.mRadialGradientScale);
                progress = (float)time_offset / 400.0F;
                canvas.save();
                interpolation = 1.0F - this.getInterpolation(progress);
                dx = this.mBitmapLoadingRectf.left + (this.mWidth / 2.0F - this.mBitmapLoadingRectf.left - 40.0F) * (1.0F - interpolation);
                float right = this.mWidth / 2.0F + 40.0F + (this.mBitmapLoadingRectf.right - this.mWidth / 2.0F - 40.0F) * interpolation;
                float top = this.mBitmapLoadingRectf.top;
                float bottom = this.mBitmapLoadingRectf.bottom;
                this.mRectFScale.set(dx, top, right, bottom);
                canvas.translate(-(this.mWidth / 2.0F - (float)LOCATION_LEFT_RIGHT_OFFSET) * (1.0F - interpolation), 0.0F);
                canvas.drawOval(this.mRectFScale, this.mPaint);
                canvas.restore();
            } else {
                if (!(this.mLoadingAnimIndex < 45.0F) && this.mLoadingAnimIndex == 45.0F) {
                    this.leftToRight = !this.leftToRight;
                    this.mLoadingAnimIndex = 0.0F;
                }

                ++this.mLoadingAnimIndex;
                progress = this.mLoadingAnimIndex / 45.0F;
                canvas.save();
                interpolation = this.getInterpolation(progress);
                dx = interpolation * (this.mWidth - (float)(2 * LOCATION_LEFT_RIGHT_OFFSET));
                if (!this.leftToRight) {
                    canvas.rotate(180.0F, this.mWidth / 2.0F, this.mHeight / 2.0F);
                }

                canvas.translate(dx, 0.0F);
                if (interpolation > 0.5F) {
                    this.mPaint.setAlpha((int)(255.0D - 255.0D * ((double)interpolation - 0.5D) * 2.0D));
                } else {
                    this.mPaint.setAlpha((int)(255.0F * interpolation * 2.0F));
                }

                canvas.drawBitmap(this.mBitmapLoading, (float)LOCATION_LEFT_RIGHT_OFFSET, this.mHeight / 2.0F - (float)this.mBitmapLoading.getHeight() / 2.0F, this.mPaint);
                canvas.restore();
            }
        }

    }

    public float getInterpolation(float input) {
        return (float)(Math.cos((double)(input + 1.0F) * 3.141592653589793D) / 2.0D) + 0.5F;
    }

    void init(Context context) {
        this.mMatrix = new Matrix();
        this.mContext = context;
        this.mPaint = new Paint();
        this.mBitmapLoading = BitmapFactory.decodeResource(this.getResources(), android.R.drawable.bottom_bar);
        this.mBitmapLogo = BitmapFactory.decodeResource(this.getResources(), android.R.drawable.bottom_bar);
        this.mPaint.setDither(true);
        this.mPaint.setAntiAlias(true);
        this.mPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
        this.mPaint.setStyle(Style.FILL_AND_STROKE);
        this.mMaskFilter = new BlurMaskFilter(1.0F, Blur.NORMAL);
        this.mRectFScale = new RectF();
        this.mPathmBitmapLoadingTop = new Path();
        this.mPathmBitmapLoadingBottom = new Path();
        this.mCursorList = new ArrayList();
        SiriCursorView red_base = new SiriCursorView(COLOR_RED, COLOR_RED_END, 1);
        this.mCursorList.add(red_base);
        SiriCursorView blue_base = new SiriCursorView(COLOR_BLUE, COLOR_BLUE_END, 1);
        this.mCursorList.add(blue_base);
        SiriCursorView green_base = new SiriCursorView(COLOR_GREEN, COLOR_GREEN_END, 1);
        this.mCursorList.add(green_base);
        SiriCursorView red_small = new SiriCursorView(COLOR_RED, COLOR_RED_END, 2);
        this.mCursorList.add(red_small);
        SiriCursorView blue_small = new SiriCursorView(COLOR_BLUE, COLOR_BLUE_END, 2);
        this.mCursorList.add(blue_small);
        SiriCursorView green_small = new SiriCursorView(COLOR_GREEN, COLOR_GREEN_END, 2);
        this.mCursorList.add(green_small);
        SiriCursorView red_small_s = new SiriCursorView(COLOR_RED, COLOR_RED_END, 3);
        this.mCursorList.add(red_small_s);
        SiriCursorView blue_small_s = new SiriCursorView(COLOR_BLUE, COLOR_BLUE_END, 3);
        this.mCursorList.add(blue_small_s);
        SiriCursorView green_small_s = new SiriCursorView(COLOR_GREEN, COLOR_GREEN_END, 3);
        this.mCursorList.add(green_small_s);
        this.mShuffleList = new ArrayList();

        for(int i = 0; i < 9; ++i) {
            this.mShuffleList.add(i);
        }

        Collections.shuffle(this.mShuffleList);
        this.reset();
    }

    void caculateLocations() {
        if (0.0F != this.mWidth) {
            for(int i = 0; i < 9; ++i) {
                SiriCursorView view = (SiriCursorView)this.mCursorList.get(i);
                view.randomLocationAndOffset();
            }
        }

    }

    void caculateDitherLocations() {
        if (0.0F != this.mWidth) {
            for(int i = 0; i < 9; ++i) {
                SiriCursorView view = (SiriCursorView)this.mCursorList.get(i);
                view.dither();
            }
        }

    }

    public void setVolume(float volume) {
        super.setVolume(volume);
        if (this.mControl == 40) {
            Collections.shuffle(this.mShuffleList);
            this.mControl = 0;
        } else if (this.mControl < 80) {
            ++this.mControl;
            if (this.mControl % 20 == 0) {
                this.caculateDitherLocations();
            }
        }

    }

    public void loading() {
        this.mLoadingAnimIndex = 0.0F;
        this.leftToRight = true;
        this.mMatrix.reset();
        this.mCurentTime = System.currentTimeMillis();
        this.mState = 2;
    }

    public void start() {
        this.mLoadingAnimIndex = 0.0F;
        this.isStopLeft = false;
        this.leftToRight = true;
        this.mState = 1;
        this.reset();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < 9; ++i) {
            SiriCursorView view = (SiriCursorView)this.mCursorList.get(i);
            sb.append("type : [" + view.type + "] ,  location: [" + view.location + "] ,  offset: [" + view.offset + "]").append("\r\n");
        }

        return sb.toString();
    }

    public void stop() {
        this.isStopLeft = false;
        this.mStopAnimIndex = 0.0F;
        this.mState = 0;
    }

    public void destory() {
        this.mState = -1;
    }

    public void stop(boolean isLeft) {
        this.isStopLeft = isLeft;
        this.mStopAnimIndex = 0.0F;
        this.mState = 0;
    }

    public void reset() {
        this.caculateLocations();
    }

    static {
        xfermode = new PorterDuffXfermode(Mode.MULTIPLY);
    }

    static class SiriCursorView {
        public static final int TYPE_CENTER = 1;
        public static final int TYPE_MIDDLE = 2;
        public static final int TYPE_OUTER = 3;
        public static final int CENTER_LOCATION_MAX = 240;
        public static final int CENTER_LOCATION_MIN = -120;
        public static final int MIDDLE_LOCATION_MAX = 360;
        public static final int MIDDLE_LOCATION_MIN = -180;
        public static final int OUTER_LOCATION_MAX = 440;
        public static final int OUTER_LOCATION_MIN = -220;
        public static final float CENTER_OFFSET_MAX = 0.95F;
        public static final float CENTER_OFFSET_MIN = 0.4F;
        public static final float MIDDLE_OFFSET_MAX = 0.6F;
        public static final float MIDDLE_OFFSET_MIN = 0.15F;
        public static final float OUTER_OFFSET_MAX = 0.3F;
        public static final float OUTER_OFFSET_MIN = 0.05F;
        Path pathTop = new Path();
        Path pathBottom = new Path();
        int color_center;
        int color_end;
        float offset = 0.3F;
        int type;
        float location;

        public SiriCursorView(int center_color, int end_color, int type) {
            this.type = type;
            this.color_end = end_color;
            this.color_center = center_color;
        }

        public void dither() {
            float offset = this.offset;
            float location = this.location;
            switch(this.type) {
                case 1:
                    float c_location = this.getRadomValue(60.0F, -30.0F);
                    if (!(location + c_location > 240.0F) && !(location + c_location < -120.0F)) {
                        location += c_location;
                    } else {
                        location -= c_location;
                    }

                    float c_offset = this.getRadomValue(0.3F, -0.15F);
                    if (!(offset + c_offset > 0.95F) && !(offset + c_offset < 0.4F)) {
                        offset += c_offset;
                    } else {
                        offset -= c_offset;
                    }
                    break;
                case 2:
                    float m_location = this.getRadomValue(60.0F, -30.0F);
                    if (!(location + m_location > 360.0F) && !(location + m_location < -180.0F)) {
                        location += m_location;
                    } else {
                        location -= m_location;
                    }

                    float m_offset = this.getRadomValue(0.3F, -0.15F);
                    if (!(offset + m_offset > 0.6F) && !(offset + m_offset < 0.15F)) {
                        offset += m_offset;
                    } else {
                        offset -= m_offset;
                    }
                    break;
                case 3:
                    float o_location = this.getRadomValue(60.0F, -30.0F);
                    if (!(location + o_location > 360.0F) && !(location + o_location < -180.0F)) {
                        location += o_location;
                    } else {
                        location -= o_location;
                    }

                    float o_offset = this.getRadomValue(0.3F, -0.15F);
                    if (!(offset + o_offset > 0.3F) && !(offset + o_offset < 0.05F)) {
                        offset += o_offset;
                    } else {
                        offset -= o_offset;
                    }
            }

            this.setLocation(location);
            this.setOffset(offset);
        }

        public void randomLocationAndOffset() {
            float offset = 0.0F;
            float location = 0.0F;
            switch(this.type) {
                case 1:
                    location = this.getRadomValue(240.0F, -120.0F);
                    offset = this.getRadomValue(0.54999995F, 0.4F);
                    break;
                case 2:
                    location = this.getRadomValue(360.0F, -180.0F);
                    offset = this.getRadomValue(0.45000002F, 0.15F);
                    break;
                case 3:
                    location = this.getRadomValue(440.0F, -220.0F);
                    offset = this.getRadomValue(0.25F, 0.05F);
            }

            this.setLocation(location);
            this.setOffset(offset);
        }

        public float getRadomValue(float max, float min) {
            return (float)(Math.random() * (double)max + (double)min);
        }

        public void setOffset(float offset) {
            if (offset > 1.0F) {
                offset = 1.0F;
            } else if (offset < 0.01F) {
                offset = 0.01F;
            }

            this.offset = offset;
        }

        public void setLocation(float location) {
            if (location > 220.0F) {
                location = 200.0F;
            } else if (location < -220.0F) {
                location = -220.0F;
            }

            this.location = location;
        }

        public void drawWave(Canvas canvas, Paint paint, float volume, float center_height, float start_x, float end_x) {
            if (AnimatedRecordingView.mGap != 0.0F) {
                canvas.save();
                paint.setColor(this.color_center);
                this.pathTop.rewind();
                this.pathTop.moveTo(start_x, center_height);
                this.pathBottom.rewind();
                this.pathBottom.moveTo(start_x, center_height);
                canvas.translate(this.location, 0.0F);

                for(int i = 0; i <= 300; ++i) {
                    float x = AnimatedRecordingView.mSamplingX[i];
                    float y_top = center_height - (float)this.caculateValue(AnimatedRecordingView.mMapX[i], volume * this.offset, true) * center_height / 2.0F;
                    this.pathTop.lineTo(x, y_top);
                    float y_bottom = center_height - (float)this.caculateValue(AnimatedRecordingView.mMapX[i], volume * this.offset, false) * center_height / 2.0F;
                    this.pathBottom.lineTo(x, y_bottom);
                }

                this.pathTop.lineTo(end_x, center_height);
                this.pathBottom.lineTo(end_x, center_height);
                canvas.drawPath(this.pathTop, paint);
                canvas.drawPath(this.pathBottom, paint);
                canvas.restore();
            }
        }

        double caculateValue(float mapX, float volume, boolean isTop) {
            float offset = volume * 1.95F / 10000.0F + 0.05F;
            offset = isTop ? offset : -offset;
            return (double)offset / Math.pow(1.0D + Math.pow((double)mapX, 2.0D), 2.0D);
        }
    }
}
